package com.supermarket.manage.business.outstock.controller;

import com.supermarket.manage.business.outstock.service.OutstockProdService;
import com.supermarket.manage.common.constant.RedisKeyConstant;
import com.supermarket.manage.common.dto.PageBean;
import com.supermarket.manage.common.dto.ResponseResult;
import com.supermarket.manage.common.dto.stock.request.StockProdParams;
import com.supermarket.manage.common.dto.stock.response.OutProdDto;
import com.supermarket.manage.common.validation.CheckGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-03-16 17:24
 **/
@RestController
@RequestMapping("/outstock/back")
@Api(value = "BackOutstockProdController", tags = "采购退货出库商品管理接口")
public class BackOutstockProdController {

    @Resource
    private OutstockProdService outstockProdService;

    @PreAuthorize("hasAnyAuthority('/','/outstock/back/get')")
    @PostMapping("/getAllProd")
    @ApiOperation(value = "获取商品分页列表", notes = "必传参数：新增单据时status为0，传递当前登录用户userId;编辑单据时status为1，传递本单据id；page，size，buyerId，（pname，cid为商品搜索条件），其他字段均与此接口无关")
    public ResponseResult<PageBean<OutProdDto>> getAllProd(@Validated({CheckGroup.Page.class, CheckGroup.Get.class})
                                                           @RequestBody StockProdParams<OutProdDto> params) {
        return outstockProdService.getAllProd(params, RedisKeyConstant.BACK_OUT);
    }

    @PreAuthorize("hasAnyAuthority('/','/outstock/back/get')")
    @PostMapping("/getAddedProd")
    @ApiOperation(value = "获取已添加的商品", notes = "必传参数：新增单据时status为0，传递当前登录用户userId;编辑单据时status为1，传递本单据id")
    public ResponseResult<List<OutProdDto>> getAddedProd(@Validated({CheckGroup.Get.class})
                                                         @RequestBody StockProdParams<OutProdDto> params) {
        return outstockProdService.getAddedProd(params, RedisKeyConstant.BACK_OUT);
    }

    @PreAuthorize("hasAnyAuthority('/','/outstock/back/manage')")
    @PostMapping("/addOutstockProd")
    @ApiOperation(value = "添加商品到出库购物车", notes = "新增单据时status为0，传递当前登录用户userId和添加的商品数组;编辑单据时status为1，传递本单据id和添加的商品数组；其他参数与此接口无关" +
            "另外，商品数组中每个商品的必要参数有pid，pnumber，subtotal，pname，unitName，pic，costPrice，salePrice,firstCname，（secondCname，thirdCname有则传，无则不传）")
    public ResponseResult<Void> addOutstockProd(@Validated({CheckGroup.Add.class})
                                                @RequestBody StockProdParams<OutProdDto> params) {
        return outstockProdService.addOutstockProd(params, RedisKeyConstant.BACK_OUT);
    }

    @PreAuthorize("hasAnyAuthority('/','/outstock/back/manage')")
    @PostMapping("/updateProd")
    @ApiOperation(value = "根据商品id修改出库购物车商品", notes = "必传参数：新增单据时status为0，传递当前登录用户userId和要修改的商品信息（pid，pnumber，subtotal）;编辑单据时status为1，传递本单据id和和要修改的商品信息（pid，pnumber，subtotal）")
    public ResponseResult<Void> updateProd(@Validated({CheckGroup.Update.class})
                                           @RequestBody StockProdParams<OutProdDto> params) {
        return outstockProdService.updateProd(params, RedisKeyConstant.BACK_OUT);
    }

    @PreAuthorize("hasAnyAuthority('/','/outstock/back/manage')")
    @PostMapping("/deleteProd")
    @ApiOperation(value = "根据商品id删除出库购物车商品",notes = "必传参数：必传参数：新增单据时status为0，传递当前登录用户userId和要删除的商品pid;编辑单据时status为1，传递本单据id和要删除的商品pid")
    public ResponseResult<Void> deleteProd(@Validated({CheckGroup.Delete.class})
                                           @RequestBody StockProdParams<OutProdDto> params) {
        return outstockProdService.deleteProd(params, RedisKeyConstant.BACK_OUT);
    }

    @PreAuthorize("hasAnyAuthority('/','/outstock/back/manage')")
    @PostMapping("/deleteAll")
    @ApiOperation(value = "清空购物车", notes = "必传参数：新增单据时status为0，传递当前登录用户userId;编辑单据时status为1，传递本单据id")
    public ResponseResult<Void> deleteAll(@Validated({CheckGroup.Get.class})
                                          @RequestBody StockProdParams<OutProdDto> params) {
        return outstockProdService.deleteAll(params, RedisKeyConstant.BACK_OUT);
    }
}
